﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Collections;

namespace 企业人事管理系统
{
    public partial class DepartTree : Form
    {
        private string rootSelect; //根的子节点获取
        private DataListForm relationForm;
        //string dySelect; //动态读取所有节点
        public DepartTree()
        {
            InitializeComponent();
            rootSelect = string.Format(
                "select DISTINCT {4} from {3},{1} where {4} not in (select {2} from {1})",
                DataName.departUP, //0
                DataName.departRelateTable, //1 
                DataName.departDown, //2
                DataName.departTable,//3
                DataName.departName //4
                );

            relationForm = new DataListForm("部门关系表",
                string.Format("select * from {0}", DataName.departRelateTable));
            relationForm.addNotClose();
        }

        private void DepartTree_Load(object sender, EventArgs e)
        {
            loadTree();
        }

        /*树的加载*/
        public void loadTree() 
        {
            TreeNode root = treeView.Nodes["root"];

            root.Nodes.Clear();

            OledbOP.initConnect(DataName.testConStr);
            OleDbDataReader rootReader = null;
            try
            {
                /*根结点的子节点加载*/
                //由于加载的是数据库中没有上级部门的数据，因此单独处理便于维护
                rootReader = OledbOP.getReader(rootSelect);
                while(rootReader.Read())
                {
                    string name = rootReader.GetString(0);
                    TreeNode temp = new TreeNode(name);
                    temp.Name = name;
                    root.Nodes.Add(temp);
                }
                rootReader.Close();
                treeView.ExpandAll();

                //层次遍历式的加载子节点
                Queue nodeQueue = new Queue();
                foreach (TreeNode child in root.Nodes)
                {
                    nodeQueue.Enqueue(child);
                }
                TreeNode nextNode = null;
                DataTable dt = OledbOP.getTable(
                    string.Format("select {0},{1} from {2}",
                    DataName.departUP,
                    DataName.departDown,
                    DataName.departRelateTable
                    ));
                while(nodeQueue.Count>0)
                {
                    nextNode = (TreeNode)nodeQueue.Dequeue();
                    // string dySelect = string.Format("select {2} from {0} where {1} = '{3}'", DataName.departRelateTable, DataName.departUP, DataName.departDown, nextNode.Name);
                    // rootReader = OledbOP.getReader(dySelect);
                    int i = 0;
                    while(i<dt.Rows.Count)
                    { 
                        if(dt.Rows[i][0].Equals(nextNode.Name))
                        {
                            string name = dt.Rows[i][1].ToString();
                            TreeNode temp = new TreeNode(name);
                            temp.Name = name;
                            nextNode.Nodes.Add(temp);
                            nodeQueue.Enqueue(temp);
                        }
                        i++;
                        //string name = rootReader.GetString(0);
                        //TreeNode temp = new TreeNode(name);
                        //temp.Name = name;
                        //nextNode.Nodes.Add(temp);
                        //nodeQueue.Enqueue(temp);
                    }
                  //  rootReader.Close();
                }
               
              
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if(rootReader !=null )
                    rootReader.Close();
            }
        }

        private void 查看部门成员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获得全公司成员信息的sql语句
            string sqlALL = string.Format(
                "select {2},{11},{8},{4},{5},{6} from {0},{1},{9} where {0}.{3} = {1}.{7}  and {0}.{13} = {9}.{10} ", 
                DataName.peopleTable,//0
                DataName.positionTable,//1
                DataName.Name,//2
                DataName.job,//3
                DataName.salary,//4
                DataName.contact,//5
                DataName.workAge,//6
                DataName.positionNumber,//7
                DataName.positionName, // 8
                DataName.departTable, //9
                DataName.departNumber, //10
                DataName.departName, // 11
                treeView.SelectedNode.Text, //12
                DataName.peoDepartNumber //13
                );//8
            //获得选定部门的成员信息的sql语句
            string sqlDepart = sqlALL + string.Format("and {9}.{11} = '{12}'",
                 DataName.peopleTable,//0
                DataName.positionTable,//1
                DataName.Name,//2
                DataName.job,//3
                DataName.salary,//4
                DataName.contact,//5
                DataName.workAge,//6
                DataName.positionNumber,//7
                DataName.positionName, // 8
                DataName.departTable, //9
                DataName.departNumber, //10
                DataName.departName, // 11
                treeView.SelectedNode.Text, //12
                DataName.peoDepartNumber //13
                );

            //显示部门成员信息窗口
            string sql = null;
            if (treeView.SelectedNode.Equals(treeView.Nodes["root"]))
                sql = sqlALL;
            else
                sql = sqlDepart;
           // MessageBox.Show(sql);
            DataListForm departMember = new DataListForm(treeView.SelectedNode.Text + "成员",
            sql);
            departMember.ShowDialog();
            departMember.Dispose();
        }

        private void 全部节点展开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView.ExpandAll();
        }

        private void 部门关系变更ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DepartChangeForm form = new DepartChangeForm(treeView.SelectedNode.Text);
            form.ShowDialog();
            form.Dispose();
            loadTree();
            treeView.Visible = true;
        }

        private void 全部节点收缩ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView.CollapseAll();

        }

        private void DepartTree_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void 查看部门关系表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            relationForm.Show();
        }
    }
}
